import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:nested_scroll_view_plus/nested_scroll_view_plus.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class WalletPage extends GetView<WalletController> {
  const WalletPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<WalletController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: _buildPageWrapper(),
        );
      },
    );
  }

  Widget _buildPageWrapper() {
    return DefaultTabController(
      length: 4,
      child: NestedScrollViewPlus(
        overscrollBehavior: OverscrollBehavior.outer,
        headerSliverBuilder: (context, innerScrolled) {
          return [
            CustomSliverBar(
              content: HeadContent(
                onWithdrawal: () {},
                onRecharge: () {},
              ),
              bottom: CustomTabBar(onTap: (int index) {
                printInfo(info: '$index');
              }),
            ),
          ];
        },
        body: _buidBody(),
      ),
    );
  }

  Widget _buidBody() {
    return SafeArea(
      top: false,
      minimum: const EdgeInsets.only(bottom: 15),
      child: SlidableAutoCloseBehavior(
        child: GetBuilder<WalletController>(
          id: 'record_list',
          builder: (_) {
            return ListView.builder(
              padding: const EdgeInsets.all(0),
              itemExtent: 40.5,
              itemCount: _.state.recordList.length,
              itemBuilder: (context, index) {
                return Column(
                  children: [
                    _buildDismissibleItem(
                      item: _.state.recordList[index],
                      child: const Cell(),
                      onDelete: (context) {
                        _.handleRemoveItem(index, context);
                      },
                    ),
                    _buildDivider(),
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }

  /// 可滑动删除的item
  Widget _buildDismissibleItem({
    required String item,
    required Widget child,
    required void Function(BuildContext) onDelete,
  }) {
    return Slidable(
      key: ObjectKey(item),
      groupTag: 'slidable',
      endActionPane: ActionPane(
        extentRatio: 0.2,
        motion: const DrawerMotion(),
        children: [
          SlidableAction(
            onPressed: onDelete,
            backgroundColor: const Color(0xFFFE4A49),
            foregroundColor: Colors.white,
            label: '删除',
            autoClose: false,
          ),
        ],
      ),
      child: child,
    );
  }

  /// 分割线
  Widget _buildDivider() {
    return const Divider(
      height: 0,
      indent: 0,
      endIndent: 0,
      thickness: 0.5,
      color: AppColors.border,
    );
  }
}
